package patrol.mobile.com.mobilepatrol.activity.bridge.regular;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.bean.Qldqjcb;
import patrol.mobile.com.mobilepatrol.listener.TimeListener;
import patrol.mobile.com.mobilepatrol.manager.QManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.widget.AddSubView;

import static patrol.mobile.com.mobilepatrol.R.id.et;
import static patrol.mobile.com.mobilepatrol.R.id.tv;

/**
 * Created by apple on 17/9/27.
 */

public class RegularCheckActivity extends BaseActivity {
//    @BindView(R.id.itme1)
//    RelativeLayout rl1;

    @BindView(R.id.itme2)
    RelativeLayout rl2;

    @BindView(R.id.itme3)
    RelativeLayout rl3;
    @BindView(R.id.itme4)
    RelativeLayout rl4;
    @BindView(R.id.itme5)
    RelativeLayout rl5;
    @BindView(R.id.itme6)
    RelativeLayout rl6;
    @BindView(R.id.itme7)
    RelativeLayout rl7;
    @BindView(R.id.itme8)
    RelativeLayout rl8;
    @BindView(R.id.itme9)
    RelativeLayout rl9;
    @BindView(R.id.itme10)
    RelativeLayout rl10;
    @BindView(R.id.itme11)
    RelativeLayout rl11;

    @BindView(R.id.itme12)
    RelativeLayout rl12;

    @BindView(R.id.itme13)
    RelativeLayout rl13;
    @BindView(R.id.itme14)
    RelativeLayout rl14;

    @BindView(R.id.itme15)
    RelativeLayout rl15;
    @BindView(R.id.item16)
    AddSubView as16;
    @BindView(R.id.item17)
    AddSubView as17;
    @BindView(R.id.item18)
    AddSubView as18;
    @BindView(R.id.itme19)
    RelativeLayout rl19;

    @BindView(R.id.itme20)
    RelativeLayout rl20;

    @BindView(R.id.itme21)
    RelativeLayout rl21;

    @BindView(R.id.itme22)
    RelativeLayout rl22;

    @BindView(R.id.itme23)
    RelativeLayout rl23;

    @BindView(R.id.bt_save)
    Button bt_save;

    @BindView(R.id.bt_list)
    Button bt_list;

    @BindView(R.id.bt_jclist)
    Button bt_jclist;

//    private EditText et1;
    private EditText et2;
    private EditText et3;
    private EditText et4;
    private EditText et5;
    private EditText et6;
    private EditText et7;
    private EditText et8;
    private EditText et9;
    private EditText et10;
    private TextView et11;
    private TextView et12;
    private TextView et13;
    private TextView et14;


    private EditText et19;
    private EditText et20;
    private EditText et21;
    private TextView et22;
    private EditText et23;
//    private RadioGroup rg1;
//    private RadioGroup rg2;
//    private RadioGroup rg3;
    private Qldqjcb bridgeDQ;
    private String qldqjcid;
    private String qlcode;
    private String[] weathers;
    String curweather;
    int curwid;
    String curtotalfen = "";
    int curtid;
    String curcleanfen;
    int curcid;
    String curserfen;
    int cursid;
    private TextView tv_weather;

    /** popup窗口里的ListView */
    private ListView mTypeLv;
    /** popup窗口 */
    private PopupWindow typeSelectPopup;

    /** 模拟的假数据 */
    private List<String> testData;

    /** 数据适配器 */
    private ArrayAdapter<String> testDataAdapter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_regular_check;
    }

    @Override
    public void setListener() {
        rl15.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 点击控件后显示popup窗口
                initSelectPopup();
                // 使用isShowing()检查popup窗口是否在显示状态
                if (typeSelectPopup != null && !typeSelectPopup.isShowing()) {
                    typeSelectPopup.showAsDropDown(tv_weather, 0, 10);
                }
            }
        });
        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                String str1 = et1.getText().toString().trim();
                //路线编码 roadcode
                String roadcode = et2.getText().toString().trim();
                //路线名称 roadname
                String roadname = et3.getText().toString().trim();
                //桥位桩号 roadpos
                String roadpos = et4.getText().toString().trim();
                //桥梁名称 qlname
                String qlname = et5.getText().toString().trim();
                //下穿通道 xctdm
                String xctdm = et6.getText().toString().trim();
                //桥长 qc
                String qc = et7.getText().toString().trim();
                //主跨结构 zkjg
                String zkjg = et8.getText().toString().trim();
                //最大跨径 zdkj
                String zdkj = et9.getText().toString().trim();
                //管养单位 gydw
                String gydw = et10.getText().toString().trim();
                //建成年月
                String jcny = et11.getText().toString().trim();
                //大中修日期
                String dzxrq = et12.getText().toString().trim();
                //上次检查日期
                String scjcrq = et13.getText().toString().trim();
                //本次检查日期
                String bcjcrq = et14.getText().toString().trim();
                //气候
                String weather = curweather;
//                //总体评分
//                String fen1 = curtotalfen;
//                //清洁
//                String fen2 = curcleanfen;
//                //保养
//                String fen3 = curserfen;
                //经常养护建议
                String jcyhjy = et19.getText().toString().trim();
                //记录人
                String jlr = et20.getText().toString().trim();
                //负责人
                String fzr = et21.getText().toString().trim();
                //下次检查时间
                String date5 = et22.getText().toString().trim();
                //缺损说明
                String str23 = et23.getText().toString().trim();
                Qldqjcb b = new Qldqjcb();
                b.qldqjcid = qldqjcid;
                b.roadcode = roadcode;
                b.roadname = roadname;
                b.roadpos = roadpos;
                b.qlname = qlname;
                b.qlcode = qlcode;
                b.xctdm = xctdm;
                b.qc = Double.valueOf(qc);
                b.zkjg = zkjg;
                b.zdkj = Double.valueOf(zdkj);
                b.gydw = gydw;

                b.jcny = jcny;
                b.scdzxrq =dzxrq;
                b.scjcrq = scjcrq;
                b.bcjcrq = bcjcrq;

                b.qh = weather;
                b.ztzkpddj = curtotalfen;
                b.qlqjzkpf = Integer.valueOf(curcleanfen);
                b.byxxzkpf = Integer.valueOf(curserfen);
                b.jcxyhjy = jcyhjy;
                b.xcjcsj = date5;
                b.jlr = jlr;
                b.fzr = fzr;
                b.qssm = str23;

                b.weather_id = curwid;
                b.fen_clean_id = curcid;
                b.fen_total_id = curtid;
                b.fen_ser_id = cursid;

                QManager.alterBridgeDQ(b);
                //修改过后改变isUp状态为false
                QManager.setBridegDQUpState(qldqjcid,false);
                QManager.setQLUpState(b.qlcode,false);
                showToast("保存成功");

            }
        });
        bt_list.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //检查桥梁id，定期检查id
                Intent intent = new Intent(RegularCheckActivity.this,BridgeRegularCheckBrokenListActivity.class);
                intent.putExtra("qldqjcid",qldqjcid);
                intent.putExtra("qlcode",qlcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(RegularCheckActivity.this,false);
            }
        });
        bt_jclist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //检查桥梁id，定期检查id
                Intent intent = new Intent(RegularCheckActivity.this,BridgeBJCheckListActivity.class);
                intent.putExtra("qldqjcid",qldqjcid);
                intent.putExtra("qlcode",qlcode);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(RegularCheckActivity.this,false);
            }
        });
        rl14.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(RegularCheckActivity.this, new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        et14.setText(date);
                    }
                });
            }
        });
//        rl11.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                CommonUtil.showDatePickDlg(RegularCheckActivity.this, new TimeListener() {
//                    @Override
//                    public void getDate(String date) {
//                        et11.setText(date);
//                    }
//                });
//            }
//        });
        rl12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(RegularCheckActivity.this, new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        et12.setText(date);
                    }
                });
            }
        });
        rl13.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(RegularCheckActivity.this, new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        et13.setText(date);
                    }
                });
            }
        });
        rl22.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.showDatePickDlg(RegularCheckActivity.this, new TimeListener() {
                    @Override
                    public void getDate(String date) {
                        et22.setText(date);
                    }
                });
            }
        });
        as16.setOnFenChangeListener(new AddSubView.OnFenChangeListener() {
            @Override
            public void onFenChange(View view, int amount) {
                curtotalfen = amount+"";
            }
        });
        as17.setOnFenChangeListener(new AddSubView.OnFenChangeListener() {
            @Override
            public void onFenChange(View view, int amount) {
                curcleanfen = amount+"";
            }
        });
        as18.setOnFenChangeListener(new AddSubView.OnFenChangeListener() {
            @Override
            public void onFenChange(View view, int amount) {
                curserfen = amount+"";
            }
        });
    }

    @Override
    public void initView() {
        super.initView();
//        TextView tv1 = rl1.findViewById(tv);
//        tv1.setText("定期检查");
//        et1 = rl1.findViewById(et);

        TextView tv2 = rl2.findViewById(tv);
        tv2.setText("路线编码");
        et2 = rl2.findViewById(et);

        TextView tv3 = rl3.findViewById(tv);
        tv3.setText("路线名称");
        et3 = rl3.findViewById(et);

        TextView tv4 = rl4.findViewById(tv);
        tv4.setText("桥位桩号");
        et4 = rl4.findViewById(et);

        TextView tv5 = rl5.findViewById(tv);
        tv5.setText("桥梁名称");
        et5 = rl5.findViewById(et);

        TextView tv6 = rl6.findViewById(tv);
        tv6.setText("下穿通道");
        et6 = rl6.findViewById(et);

        TextView tv7 = rl7.findViewById(tv);
        tv7.setText("桥  长");
        et7 = rl7.findViewById(et);

        TextView tv8 = rl8.findViewById(tv);
        tv8.setText("主跨结构");
        et8 = rl8.findViewById(et);

        TextView tv9 = rl9.findViewById(tv);
        tv9.setText("最大跨径");
        et9 = rl9.findViewById(et);

        TextView tv10 = rl10.findViewById(tv);
        tv10.setText("管养单位");
        et10 = rl10.findViewById(et);

        TextView tv11 = rl11.findViewById(tv);
        tv11.setText("建成年月");
        et11 = rl11.findViewById(R.id.tv1);

        TextView tv12 = rl12.findViewById(tv);
        tv12.setText("大中修日期");
        et12 = rl12.findViewById(R.id.tv1);

        TextView tv13 = rl13.findViewById(tv);
        tv13.setText("上次检查日期");
        et13 = rl13.findViewById(R.id.tv1);

        TextView tv14 = rl14.findViewById(tv);
        tv14.setText("本次检查日期");
        et14 = rl14.findViewById(R.id.tv1);


        TextView tv15 = rl15.findViewById(tv);
        tv15.setText("气候");
        tv_weather = rl15.findViewById(R.id.tv1);



        as16.setTv("总体状况评分");


        as17.setTv("清洁状况评分");

        as18.setTv("保养状况评分");

        TextView tv19 = rl19.findViewById(tv);
        tv19.setText("经常养护建议");
        et19 = rl19.findViewById(et);

        TextView tv20 = rl20.findViewById(tv);
        tv20.setText("记录人");
        et20 = rl20.findViewById(et);

        TextView tv21 = rl21.findViewById(tv);
        tv21.setText("负责人");
        et21 = rl21.findViewById(et);

        TextView tv22 = rl22.findViewById(tv);
        tv22.setText("下次检查时间");
        et22 = rl22.findViewById(R.id.tv1);

        TextView tv23 = rl23.findViewById(tv);
        tv23.setText("缺损说明");
        et23 = rl23.findViewById(et);

    }

    @Override
    public void initData() {
        super.initData();
        Resources resources = getResources();
        weathers = resources.getStringArray(R.array.weather);
//        fens = new String[]{"5", "8", "10"};
         qldqjcid = getIntent().getStringExtra("qldqjcid");
         qlcode = getIntent().getStringExtra("qlcode");
         bridgeDQ = QManager.getBridgeDQ(qlcode,qldqjcid);

        setData();

    }

    private void setData() {
//        et1.setText("定期检查");

        et2.setText(bridgeDQ.roadcode);

        et3.setText(bridgeDQ.roadname);

        et4.setText(bridgeDQ.roadpos+"");

        et5.setText(bridgeDQ.qlname);

        et6.setText(bridgeDQ.xctdm);

        et7.setText(bridgeDQ.qc+"");

        et8.setText(bridgeDQ.zkjg);

        et9.setText(bridgeDQ.zdkj+"");

        et10.setText(bridgeDQ.gydw);

        et11.setText(bridgeDQ.jcny);
        et12.setText(bridgeDQ.scdzxrq);

        et13.setText(bridgeDQ.scjcrq);

        et14.setText(bridgeDQ.bcjcrq);

        et20.setText(bridgeDQ.jlr);

        et21.setText(bridgeDQ.fzr);

        et22.setText(bridgeDQ.xcjcsj);

        et23.setText(bridgeDQ.qssm);
        //天气
        int id_weather = bridgeDQ.weather_id;
        if (id_weather!=-1){
            tv_weather.setText(weathers[id_weather]);
            curweather = weathers[id_weather];
        }else{
            tv_weather.setText(weathers[0]);
            curweather = weathers[0];
        }

        if (!bridgeDQ.ztzkpddj.equals("")){
            as16.setFen(Integer.valueOf(bridgeDQ.ztzkpddj));
            curtotalfen = bridgeDQ.ztzkpddj;
        }else{
            as16.setFen(Integer.valueOf(1));
            curtotalfen = 1+"";
        }

        as17.setFen(Integer.valueOf(bridgeDQ.qlqjzkpf));
        curcleanfen = bridgeDQ.qlqjzkpf+"";
        as18.setFen(Integer.valueOf(bridgeDQ.byxxzkpf));
        curserfen = bridgeDQ.byxxzkpf+"";

    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(RegularCheckActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("定期检查");
    }
    /**
     * 初始化popup窗口
     */
    private void initSelectPopup() {
        mTypeLv = new ListView(this);
        weatherData();
        // 设置适配器
        testDataAdapter = new ArrayAdapter<String>(this, R.layout.popup_text_item, testData);
        mTypeLv.setAdapter(testDataAdapter);

        // 设置ListView点击事件监听
        mTypeLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // 在这里获取item数据
                String value = testData.get(position);
                // 把选择的数据展示对应的TextView上
                tv_weather.setText(value);
                curweather = value;
                curwid = position;
                // 选择完后关闭popup窗口
                typeSelectPopup.dismiss();
            }
        });
        typeSelectPopup = new PopupWindow(mTypeLv, tv_weather.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg1);
        typeSelectPopup.setBackgroundDrawable(drawable);
        typeSelectPopup.setFocusable(true);
        typeSelectPopup.setOutsideTouchable(true);
        typeSelectPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                typeSelectPopup.dismiss();
            }
        });
    }

    private void weatherData() {
        testData = new ArrayList<>();
        for (int i = 0; i < weathers.length; i++) {
            String str = weathers[i];
            testData.add(str);
        }
    }
}
